package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.BusiSegmentTypeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 细分业务类型联动设值
 * Created by  huanghaosheng on 2020/11/25 14:53
 */
@Service
@AttrHandler(BusiSegmentTypeAttrHandler.class)
public class BusiSegmentTypeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {


    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        Optional.ofNullable(inst.getAttr("200004009"))
            .ifPresent(kiteAttr ->{
                if(!KiteStringUtils.isEqual(KeyConsts.PROD_ATTR_SHOW_TYPE_E,kiteAttr.getShowType())){
                    if(KiteStringUtils.isEqual("1_0000107",kiteAttr.getValue())){
                        Optional.ofNullable(inst.getAttr("200004010")).ifPresent(
                            funcAttr -> {
                                funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                                funcAttr.setIsNull(KeyConsts.IFTRUE_F);
                            });
                    }else{
                        if(KiteStringUtils.isEqual("100006962",attr.getValue())){
                            Optional.ofNullable(inst.getAttr("200004009")).ifPresent(
                                funcAttr -> {
                                    if(KiteStringUtils.isEqual("1_0000106",funcAttr.getValue())){
                                        Optional.ofNullable(inst.getAttr("200004012")).ifPresent(
                                            s -> {
                                                s.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                                                s.setIsNull(KeyConsts.IFTRUE_T);
                                            });
                                        Optional.ofNullable(inst.getAttr("200004011")).ifPresent(
                                            s -> {
                                                s.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                                                s.setIsNull(KeyConsts.IFTRUE_F);
                                            });
                                    }
                                });
                        }else{
                            Optional.ofNullable(inst.getAttr("200004012")).ifPresent(
                                s -> {
                                    s.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                                    s.setIsNull(KeyConsts.IFTRUE_F);
                                });
                            Optional.ofNullable(inst.getAttr("200004011")).ifPresent(
                                s -> {
                                    s.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                                    s.setIsNull(KeyConsts.IFTRUE_F);
                                });
                        }
                    }
                }else{
                    Optional.ofNullable(inst.getAttr("200004010")).ifPresent(
                        funcAttr -> {
                            funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                            funcAttr.setIsNull(KeyConsts.IFTRUE_T);
                        });
                    Optional.ofNullable(inst.getAttr("200004011")).ifPresent(
                        funcAttr -> {
                            funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                            funcAttr.setIsNull(KeyConsts.IFTRUE_T);
                        });
                    Optional.ofNullable(inst.getAttr("200004012")).ifPresent(
                        funcAttr -> {
                            funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                            funcAttr.setIsNull(KeyConsts.IFTRUE_T);
                        });
                }
            });
        Optional.ofNullable(inst.getAttr("200004013"))
            .ifPresent(kiteAttr ->{
                if(KiteStringUtils.isEqual(KeyConsts.PROD_ATTR_SHOW_TYPE_E,kiteAttr.getShowType())){
                    Optional.ofNullable(inst.getAttr("200004015")).ifPresent(
                        funcAttr -> {
                            funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                            funcAttr.setIsNull(KeyConsts.IFTRUE_T);
                        });
                    Optional.ofNullable(inst.getAttr("200004016")).ifPresent(
                        funcAttr -> {
                            funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                            funcAttr.setIsNull(KeyConsts.IFTRUE_T);
                        });
                }else{
                    if(KiteStringUtils.isEqual("1_0000106",kiteAttr.getValue())){
                        Optional.ofNullable(inst.getAttr("200004015")).ifPresent(
                            funcAttr -> {
                                funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                                funcAttr.setIsNull(KeyConsts.IFTRUE_F);
                            });
                        Optional.ofNullable(inst.getAttr("200004016")).ifPresent(
                            funcAttr -> {
                                funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                                funcAttr.setIsNull(KeyConsts.IFTRUE_T);
                            });
                    }
                }
            });

        Optional.ofNullable(inst.getAttr("200004017"))
            .ifPresent(kiteAttr ->{
                if(!KiteStringUtils.isEqual(KeyConsts.PROD_ATTR_SHOW_TYPE_E,kiteAttr.getShowType())){
                    if(KiteStringUtils.isEqual("1_0000106",kiteAttr.getValue())){
                        Optional.ofNullable(inst.getAttr("200003870")).ifPresent(
                            funcAttr -> {
                                funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                                funcAttr.setIsNull(KeyConsts.IFTRUE_F);
                            });
                    }else{
                        Optional.ofNullable(inst.getAttr("200003870")).ifPresent(
                            funcAttr -> {
                                funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                                funcAttr.setIsNull(KeyConsts.IFTRUE_T);
                            });
                    }
                }else{
                    Optional.ofNullable(inst.getAttr("200003870")).ifPresent(
                        funcAttr -> {
                            funcAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                            funcAttr.setIsNull(KeyConsts.IFTRUE_T);
                        });
                }
            });
        // 客户公网IP
        Optional.ofNullable(inst.getAttr("200004007")).ifPresent(kiteAttr -> {
                KiteAttr apnAttr = inst.getAttr("200003999"); //APN类型
                // APN类型选择VPDN 细分业务类型选择互联网GRE或互联网L2TP 时，客户公网IP必填
                if (KiteStringUtils.equals("100006957", apnAttr.getValue()) && KiteStringUtils.equalsAny(attr.getValue(), "100006960", "100006961")) {
                    kiteAttr.setIsNull(KeyConsts.IFTRUE_F);
                } else {
                    kiteAttr.setIsNull(KeyConsts.IFTRUE_T);
                }
            }
        );

        // VLAN号
        Optional.ofNullable(inst.getAttr("200004242")).ifPresent(kiteAttr -> {
                KiteAttr apnAttr = inst.getAttr("200003999"); //APN类型
                // APN类型选择VPDN 细分业务类型选择专线GRE或专线L2TP 时，VLAN号选填
                if (KiteStringUtils.equals("100006957", apnAttr.getValue()) && KiteStringUtils.equalsAny(attr.getValue(), "100006962", "100006963")) {
                    kiteAttr.setIsNull(KeyConsts.IFTRUE_T);
                } else {
                    kiteAttr.setIsNull(KeyConsts.IFTRUE_F);
                }
            }
        );

        //是否固定ip
        Optional.ofNullable(inst.getAttr("200004084")).ifPresent(kiteAttr -> {
                KiteAttr apnTypeAttr = inst.getAttr("200003999"); //APN类型
            /**
             * 1、APN类型选择VPDN 细分业务类型选择互联网GRE或专线GRE 时，是否固定ip可编辑
             * 2、APN类型选择MEC 细分业务类型选择MEC 时，是否固定ip可编辑
             */
            if (KiteStringUtils.equals("100006957", apnTypeAttr.getValue()) && KiteStringUtils.equalsAny(attr.getValue(), "100006960", "100006962")) {
                kiteAttr.setIsEdit(KeyConsts.IFTRUE_T);
            }
            else if (KiteStringUtils.equals("100007260", apnTypeAttr.getValue()) && KiteStringUtils.equals("100007260", attr.getValue())) {
                kiteAttr.setIsEdit(KeyConsts.IFTRUE_T);
            }
            else {
                kiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
            }
        });
    }
}
